package com.coocaa.ops.admin.v1.policygroup;

import com.coocaa.ops.admin.base.annotation.Auth;
import com.coocaa.ops.admin.base.annotation.Config;
import com.coocaa.ops.admin.base.controller.MgrBaseController;
import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.base.util.WebUtil;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.policygroup.model.HomeMatchQueryModel;
import com.coocaa.ops.admin.v1.policygroup.model.query.PolicyGroupInfoModel;
import com.coocaa.ops.admin.v1.policygroup.service.IPolicyGroupInfoQueryService;
import com.coocaa.ops.admin.v1.policygroup.service.IPolicyGroupInfoUpdateService;
import com.coocaa.ops.admin.v1.policygroup.validator.PolicyGroupValidator;
import com.coocaa.ops.common.core.base.enums.AuthEnum;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.biz.policygroup.entity.PolicyGroupInfoEntity;
import com.coocaa.ops.common.tool.json.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author bjh
 * @description 策略组信息原始表-前端控制器
 * @date 2020-03-12 14:46:04
 */
@RestController
@Config(menuToken = "policyGroup")
@RequestMapping("/v1/policyGroup")
public class PolicyGroupInfoController extends MgrBaseController {

    @Autowired
    private IPolicyGroupInfoUpdateService thisUpdateService;
    @Autowired
    private IPolicyGroupInfoQueryService thisQueryService;
    private static CommonLanguageProperties commonLanguageProperties;

    @RequestMapping(value = "/listPolicyGroupByModelChip", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String listPolicyGroupByModelChip(HttpServletRequest request, String modelChip) {
        Assert.hasText(modelChip, "modelChip" + commonLanguageProperties.getNotNull());
        R result = R.ok(thisQueryService.listPolicyGroupByModelChip(modelChip));
        return JsonUtil.obj2JsonDisableCircularRef(result);

    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        PolicyGroupInfoController.commonLanguageProperties = commonLanguageProperties;
    }

    /**
     * 保存
     *
     * @param jsonStr
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.SAVE)
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Object save(@RequestParam String jsonStr, HttpServletRequest request) {
        PolicyGroupInfoEntity entity = (PolicyGroupInfoEntity) JsonUtil.json2Object(jsonStr, PolicyGroupInfoEntity.class);
        PolicyGroupValidator.validBeforeSave(entity);
        thisUpdateService.save(entity, WebUtil.getCurrentUser(request));
        return R.ok();

    }

    /**
     * 分页数据显示-菜单自己使用
     *
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/pageList", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String pageList(HttpServletRequest request, PolicyGroupInfoModel model) {
        PageResultModel<PolicyGroupInfoEntity> pageResult = thisQueryService.pageList(model);
        R result = R.ok(pageResult);
        return JsonUtil.obj2JsonDisableCircularRef(result);

    }


    /**
     * 获取详情信息
     *
     * @param id
     * @param version
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getDetailInfo", method = RequestMethod.GET)
    public Object getDetailInfo(@RequestParam Integer id, @RequestParam(required = false) Integer version) {
        InputParamsValidator.validIdValue(id);
        PolicyGroupInfoEntity entity = thisQueryService.getDetailInfo(id, version);
        Map result = R.ok().data(entity);
        return render2Json(result);

    }

    /**
     * 删除副本
     *
     * @param request
     * @param id
     * @param version
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/delHistory", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public R delHistory(HttpServletRequest request, Integer id, Integer version) {
        InputParamsValidator.validIdAndVersion(id, version);
        thisUpdateService.deleteHistory(id, version, WebUtil.getCurrentUser(request));
        return R.ok();
    }

    /**
     * 删除记录
     *
     * @param
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.DELETE)
    @RequestMapping(value = "/remove", method = {RequestMethod.GET, RequestMethod.POST})
    public Object remove(Integer[] id, HttpServletRequest request) {
        Assert.notEmpty(id, "id" + commonLanguageProperties.getNotNull());
        return thisUpdateService.deleteBatch(id, WebUtil.getCurrentUser(request));

    }

    /**
     * 获取菜单运行权限列表
     *
     * @param request
     * @param
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getMenuInfo", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public Object getMenuInfo(HttpServletRequest request) {
        Map result = getMenuInfo(this.getClass(), request);
        return render2Json(result);
    }

    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/matchHomepage", method = RequestMethod.GET)
    public R matchHomepage(HttpServletRequest request, HomeMatchQueryModel model) throws Exception {
        PolicyGroupValidator.valid(model);
        return thisQueryService.matchHomepage(model);
    }
}
